package com.tt.job.runner;

import com.tt.job.register.JobRegistryCenter;
import com.tt.job.service.ClusterJobService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 将内部支持的任务信息同步至注册中心
 */
@Order(1)
@Component
public class JobLoaderRunner implements ApplicationRunner {

    private final static Logger logger = LoggerFactory.getLogger(JobLoaderRunner.class);
    @Autowired
    private ClusterJobService jobService;
    @Autowired
    private JobRegistryCenter registryCenter;

    @Override
    public void run(final ApplicationArguments applicationArguments) throws Exception {
        //与注册中心建立连接
        registryCenter.start();
        logger.debug("完成与注册中心的连接，同步本地任务及获取注册中心任务信息...");
        //进行job信息的本地获取并与注册中心对比
        //异步启动，主程序将SpringUtils中的context填充，方便程序内获取bean对象，否则会返回null
        new Thread(() -> jobService.start(registryCenter)).start();
    }

}
